clear all
cd "C:\Users\Public\Documents\ImmPanelRevis19\"
cd "ImmPanelRevis19\Donnees\select"

/*************************/
/* Employment unbalanced */
/*************************/
capture program drop emplcs
program define emplcs
version 11
syntax, occp(name)

use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\select\unbalanced.dta", clear
keep if `occp'==1

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
gen ldp=ln(dp)
/* residual dp for each year */
quietly: reg ldp F_* if ptreat | dtreat
predict rdp, residuals

save temp1.dta, replace
collapse (mean) prdp=rdp if ptreat, by(ze an)
save p1.dta, replace
use temp1.dta, clear
collapse (mean) drdp=rdp if dtreat, by(ze an)
save p2.dta, replace
use temp1.dta, clear
collapse dimm dimm75 Y_*, by(ze an)
joinby ze an using p1.dta, unm(m)
drop _merge
joinby ze an using p2.dta, unm(m)
drop _merge

egen ize=group(ze)
egen time=group(an)
xtset ize time
gen rdp=drdp-L.prdp
/* OLS */
ivreg2 rdp dimm Y_*  , partial(Y_* ) cl(ze)
estimates store `occp'ols

/* IV */
ivreg2 rdp (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store `occp'iv
end

emplcs, occp(all) 
emplcs, occp(cad) 
emplcs, occp(tecemp2) 
emplcs, occp(bcq) 
emplcs, occp(bcnq) 

capture rm "C:\Users\Public\Documents\Tab21\Table8.rtf"
/* OLS */
esttab allols  cadols tecemp2ols bcqols bcnqols ///
using "C:\Users\Public\Documents\Tab21\Table8.rtf" /// 
, append title("OLS_CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/* IV */
esttab alliv  cadiv tecemp2iv bcqiv bcnqiv ///
using "C:\Users\Public\Documents\Tab21\Table8.rtf" /// 
, append title("2SLS-CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/*************************************************************/
/* regression de log annual days worked weighted */
/*************************************************************/
capture program drop dp
program define dp
version 11
syntax, occp(name) 

/********************/
/* Balanced Sample  */
/********************/
use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\select\final.dta", clear

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsn=ln(dp)
/* residual wages for each year */
quietly: reg lsn F_* 
predict rlsn, residuals 
/* change in residual wages */
gen drlsn=D.rlsn 

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsn */
quietly: sum drlsn if an==`year', d
replace drlsn=. if drlsn>`r(p99)' & an==`year'
replace drlsn=. if drlsn<`r(p1)' & an==`year'
}
drop if missing(drlsn)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
gen dummy=1
collapse (sum) nbze = dummy (mean) drlsn dimm dimm75 Y_*, by(ze an)

/* OLS */
ivreg2 drlsn dimm Y_*  , partial(Y_* ) cl(ze)
estimates store bs`occp'ols

/* IV */
ivreg2 drlsn (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store bs`occp'iv

gen sqnbze=sqrt(nbze)

/* IV */
ivreg2 drlsn (dimm  = dimm75 ) Y_* [aweight = sqnbze] , partial(Y_* ) cl(ze)
estimates store wbs`occp'iv

end

dp, occp(all) 
dp, occp(cad) 
dp, occp(tecemp2) 
dp, occp(bc) 
dp, occp(bcnq) 

/*******************************/
/* regression de log annual days worked weighted */
/*******************************/
/* Unweighted */

/* OLS Balanced */
esttab bsallols  bscadols bstecemp2ols bsbcols bsbcnqols ///
using "C:\Users\Public\Documents\Tab21\Table8.rtf" /// 
, append title("dp 2SLS unweighted")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N) star(* 0.10 ** 0.05 *** 0.01)
 
/* IV Balanced */
esttab bsalliv  bscadiv bstecemp2iv bsbciv bsbcnqiv ///
using "C:\Users\Public\Documents\Tab21\Table8.rtf" /// 
, append title("dp 2SLS unweighted")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N) star(* 0.10 ** 0.05 *** 0.01)
 
